<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * 订单以及售后中心
 * @author soubao 当燃
 *  @Date: 2016-12-20
 */
namespace app\home\controller;
use app\common\logic\CommentLogic;
use think\Db;
use think\Page;

class Saleorder extends Base {

	public $user_id = 0;
	public $user = array();

    public function _initialize() {      
        parent::_initialize();
        if(session('?user'))
        {
        	$user = session('user');        
        	$this->user = $user;
        	$this->user_id = $user['user_id'];
        	$this->assign('user',$user); //存储用户信息
        	$this->assign('user_id',$this->user_id);
/*            //获取用户信息的数量
            $messageLogic = new MessageLogic();
            $user_message_count = $messageLogic->getUserMessageCount();
            $this->assign('user_message_count', $user_message_count);*/
        }else{
            redirect()->remember();
            $this->redirect('User/login');
        }
        //用户中心面包屑导航
        $navigate_user = navigate_user();
        $this->assign('navigate_user',$navigate_user);        
    }
    
    /*
     * 评论晒单
     */
    public function comment()
    {
        $user_id = $this->user_id;
        $status = I('get.status', -1);
        $logic = new CommentLogic;
        $data = $logic->getComment($user_id, $status); //获取评论列表
        $this->assign('page', $data['page']);// 赋值分页输出
        $this->assign('comment_page', $data['page']);
        $this->assign('comment_list', $data['result']);
        $this->assign('active', 'comment');
        return $this->fetch();
    }
    

    /*
     * 用户拍卖交保证金
     */
    public function cash(){

        if(IS_POST){
            $user = session('user');  
            $data['uid'] = $user['user_id'];     
            $data['goods_id'] = I('post.goods_id');
            $data['state'] = 1;
            $users = M('users')->field('user_money')->where('user_id',$data['uid'])->find();
            $money= I('post.money');
            if($money > $users['user_money']){
                $this->ajaxReturn(['status' => -1, 'msg' => '余额不足，前往充值...','url'=>U('Home/User/recharge')]); 
            }
            $info = M('cash')->where($data)->find();
            if($info){
                $this->ajaxReturn(['status' => -1, 'msg' => '你已交过保证金']);
            }else{   
                $data['money'] = I('post.money');
                $data['addtime'] = date('Y-m-d H:i:s');
                $msg['user_id'] =  $data['uid'];
                $msg['user_money'] =  array('exp','user_money-'.$money);
                $moneys = M('users')->where('user_id',$data['uid'])->setDec('user_money',$money);
                $cash = DB::name('cash')->insert($data);
                if($cash && $moneys){
                    $this->ajaxReturn(['status' => 1, 'msg' => '保证金交费成功']); 
                }else{
                    $this->ajaxReturn(['status' => -1, 'msg' => '保证金交费']); 
                }
            }
        }
    }

    /*
     * 拍卖
     */ 
    public function orders(){
        $user = session('user'); 
        $data['uid'] = $user['user_id'];
        if(IS_POST){ 
            $data['orderid'] = date("YmdHis").rand(0,9999);
            $data['goods_id'] =  I('post.goods_id');
            $order = M('sale_order')->where($data)->find();
            $data['money'] = trim(I('post.money'));
            $data['ordertime'] = time();
            $good = M('sale')->field('shop_price,now_price,market_price,add_price,count')->where('id',$data['goods_id'])->find();

            //计算拍卖人数
            if($data['money'] < ($good['now_price'] + $good['add_price'])){
                $this->ajaxReturn(['status' => -1, 'msg' => '拍卖金额不能小于当前价加起拍价梯度']);
            }
            $msg['uname'] = $user['nickname'];
            $msg['now_price'] = $data['money'];
            $msg['uids'] = $data['uid'];
            if(!$order){
               $msg['count'] = $good['count']+1;
            }
            $info = M('sale_order')->insertGetId($data);
            $msg['order_id'] = $info;
            $sale = M('sale')->where('id',$data['goods_id'])->update($msg);
            if($info && $sale){
                $this->ajaxReturn(['status' => 1, 'msg' => '加价成功']);
            }else{
                $this->ajaxReturn(['status' => -1, 'msg' => '加价失败']);
            }
        }     
    }

   
}